var verse;
var chapter;
var book;
var bookName;
var bookUrl;
var chapterNumber;
var imageUrl;
var choice;
var verseCounter;
var chapterCounter;
var xmlDomObj;
var html;
var html_sk;
var text;
var alt;
var tabs;
var englishTab;
var sanskritTab;
var lastVerseNumber;
function initialise() {
	verse = null;
	chapter = null;
	book = null;
	bookName = null;
	bookUrl = null;
	chapterNumber = null;
	imageUrl = null;
	choice = null;
	verseCounter = null;
	chapterCounter = null;
	xmlDomObj = null;
	html = null;
	html_sk = null;
	text = null;
	alt = null;
	lastVerseNumber = null;
	bookNumber = null;
	tabs = new _IG_Tabs(__MODULE_ID__, "English");
	englishTab = tabs.addTab("English");
	sanskritTab = tabs.addTab("Sanskrit");
	displayQuote();
}
function displayQuote() {
	var url = "http://hosting.gmodules.com/ig/gadgets/file/107661947689363314762/data.xml";
	var prefs = new _IG_Prefs(__MODULE_ID__);
	choice = prefs.getInt("myChoice");
	verseCounter = prefs.getInt("verseCounter");
	chapterCounter = prefs.getInt("chapterCounter");
	lastVerseNumber = prefs.getInt("lastVerseNumber");
	_IG_FetchXmlContent(url, processXMLData);
	prefs.set("verseCounter", verseCounter);
	prefs.set("chapterCounter", chapterCounter);
	prefs.set("lastVerseNumber", lastVerseNumber);
}
function processXMLData(response) {
	if (response == null || typeof(response) != "object"
			|| response.firstChild == null) {
		_gel("content_div").innerHTML = "<i>Invalid data.</i>";
		return;
	}
	xmlDomObj = response;
	if (choice == 0) {
		getRandomVerse(response);
	} else {
		if (choice == 1) {
			getSequentialVerse(response);
		} else {
			bookNumber = choice -2;
			getFirstQuote();
		}
	}
}
function displayScreen() {
	html = "";
	html_sk = "";
	var sanskrit = null;
	html = "<div style='text-align:center; font-size: 120%; "
			+ "font-weight: 700;'>";
	var title = chapter.getAttribute("name");
	html += title + "</div><br>";
	html += "<table><tr valign='top' >";
	imageUrl = chapter.getAttribute("image");
	alt = chapter.getAttribute("alt");
	html += "<td><img src='" + imageUrl + "' width='75' height='100' alt='"
			+ alt + "'></td>";
	var verseNumber = verse.getAttribute("number");
	var data = text.firstChild.data;
	html += "<td style='font-size: 75%;'><i>" + "'" + data + "'"
			+ "</i></td></tr>";
	html += "<tr align=right><td>&nbsp;</td>";
	html += "<td style='font-size:70%;'>- " + "<a href='" + bookUrl
			+ "' target='_blank' >" + bookName + "</a>" + ", " + chapterNumber
			+ "." + verseNumber + "</td></tr>";
	html += "</table>";
	if (bookNumber !=null && bookNumber >= 0) {
		html += "<table><tr valign='top' >";
		html += "<td><input type='button' value='First' name='first' onClick='getFirstQuote()' /> </td>";
		html += "<td><input type='button' value='Prev' name='prev' onClick='getPreviousQuote()'/> </td>";
		html += "<td><input type='button' value='Next' name='next' onClick='getNextQuote()'/> </td>";
		html += "<td><input type='button' value='Last' name='last' onClick='getLastQuote()'/> </td>";
		html += "</tr></table>" ;
	}	
	html += "Choice=" + choice + " Book Number=" + bookNumber;
	_gel(englishTab).innerHTML = html;
	var sanskrit_section = verse.getElementsByTagName("sanskrit");
	if (sanskrit_section.length <= 0)
		html_sk = "<br><br><br>&nbsp;&nbsp;<i>No sanskrit available for this verse</i>";
	else {
		var sanskrit = sanskrit_section[0];
		html_sk += "<table><tr valign='top'>";
		html_sk += "<td><img src='" + sanskrit.getAttribute("text")
				+ "'alt='Sanskrit Text'></td></tr>";
		html_sk += "<tr><td style='font-size:70%;'> <a href='http://www.sanskrit.org/www/Sanskrit/Transliteration.htm' target='_blank'><i>Transliteration</i></a></td></tr>";
		html_sk += "<tr><td><img src='"
				+ sanskrit.getAttribute("transliteration")
				+ "'alt='Transliteration'></td></tr>";
		html_sk += "<tr><td style='font-size:70%;'> <a href='http://www.sanskrit-lexicon.uni-koeln.de/monier/index.html' target='_blank'><i>Word Meanings</i></a></td></tr>";
		html_sk += "<tr><td><img src='" + sanskrit.getAttribute("anvaya")
				+ "'alt='Word/Phrase Meanings'></td></tr>";
		html_sk += "<tr><td style='font-size:70%;'> <a href='http://www.tilakpyle.com/sanskrit_alphabet.htm' target='_blank'><i>Vocals / Pronounciation</i></a></td></tr>";
		html_sk += "<tr><td><embed src='"
				+ sanskrit.getAttribute("vocals")
				+ "' autostart='true' loop='false' width='350' height='25'></embed></td></tr></table>";
	}
	_gel(sanskritTab).innerHTML = html_sk;
}
function rand(n) {
	if (n <= 1)
		return 0;
	var rann = (Math.floor(Math.random() * n + 1));
	if (rann <= 1)
		return 0;
	return rann;
}
function getNextQuote() {
	verseCounter = verseCounter + 1;
	getBookVerse(getBook(xmlDomObj));
	displayScreen();
}
function getFirstQuote() {
	verseCounter = 0;
	chapterCounter = 0;
	getBookVerse(getBook(xmlDomObj));
	displayScreen();
}
function getPreviousQuote() {
	verseCounter = verseCounter - 1;
	if (verseCounter < 0)
		verseCounter = 0;
	getBookVerse(getBook(xmlDomObj));
	displayScreen();
}
function getLastQuote() {
	var book = getBook(xmlDomObj);
	var desc = book.getElementsByTagName("description");
	verseCounter = desc.length - 1;
	getBookVerse(book);
	displayScreen();
}
function getRandomVerse(response) {
	verseTexts = response.getElementsByTagName("description");
	randomText = rand(verseTexts.length);
	text = verseTexts.item(randomText);
	verse = text.parentNode;
	chapter = verse.parentNode;
	book = chapter.parentNode;
	bookName = book.getAttribute("name");
	bookUrl = book.getAttribute("url");
	chapterNumber = chapter.getAttribute("number");
	displayScreen();
}
function getVerseByBook(response) {
	var books = response.getElementsByTagName("book");
	book = books[bookNumber];
	bookName = book.getAttribute("name");
	bookUrl = book.getAttribute("url");
	var chapters = book.childNodes;
	chapter = chapters[chapterCounter];
	chapterNumber = chapter.getAttribute("number");
	var verses = chapter.childNodes;
	if (verseCounter > verses.length)
		verseCounter = verses.length;
	verse = verses[verseCounter];
	text = verse.firstChild;
}
function getBookVerse(response) {
	verseTexts = response.getElementsByTagName("description");
	text = verseTexts[verseCounter];
	if (text == null || typeof(text) != "object" || text.firstChild == null) {
		verseCounter = verseCounter - 1;
		text = verseTexts[verseCounter];
	}
	verse = text.parentNode;
	chapter = verse.parentNode;
	book = chapter.parentNode;
	bookName = book.getAttribute("name");
	bookUrl = book.getAttribute("url");
	chapterNumber = chapter.getAttribute("number");
}
function getBook(xmlDomObj) {
	var docRoot = xmlDomObj.documentElement;
	var books = xmlDomObj.getElementsByTagName("book");
	for (i = 0; i < books.length; i++) {
		book = books[i];
		var index = parseInt(book.getAttribute("index"));
		if (index != bookNumber)
			docRoot.removeChild(book);
	}
	return xmlDomObj;
}
function getSequentialVerse(response) {
	verseTexts = response.getElementsByTagName("description");
	if (lastVerseNumber == null || lastVerseNumber == ""
			|| lastVerseNumber >= verseTexts.length)
		lastVerseNumber = 0;
	else
		lastVerseNumber = lastVerseNumber + 1;
	text = verseTexts.item(lastVerseNumber);
	verse = text.parentNode;
	chapter = verse.parentNode;
	book = chapter.parentNode;
	bookName = book.getAttribute("name");
	bookUrl = book.getAttribute("url");
	chapterNumber = chapter.getAttribute("number");
	displayScreen();
}
_IG_RegisterOnloadHandler(initialise);